# -*- coding: utf-8 -*-
from tkinter import *
from tkinter.messagebox import showinfo, showerror
import os
import shutil

class MY_GUI():
    def __init__(self, init_window_name):
        self.init_window_name = init_window_name

    # 设置窗口
    def set_init_window(self):
        self.init_window_name.title("发布路径处理工具_v1.0")  # 窗口名
        self.init_window_name.geometry('500x400+200+100')    #500x400为窗口大小，+200 +100 定义窗口弹出时的默认展示位置
        # 标签
        self.source_path_label = Label(
            self.init_window_name, text="待处理路径:").pack()
        # 文本框
        self.source_path = Entry(self.init_window_name, width=50)
        self.source_path.pack()
        self.target_path_label = Label(
            self.init_window_name, text="处理后路径:").pack()
        self.target_path = Entry(self.init_window_name, width=50)
        self.target_path.pack()
        # 按钮
        self.process_button = Button(self.init_window_name, text="路径转换", bg="lightblue", width=10, command=self.cut_folder) # 调用内部方法,加()为直接调用
        self.process_button.pack()

    def cut_folder(self):
        path1 = self.source_path.get() # 画面录入的待处理路径
        path2 = self.target_path.get() # 画面录入的处理后路径

        try:
            if os.path.exists(path2):
                shutil.rmtree(path2)
            
            # print(os.listdir(path1))
            for comp in os.listdir(path1): # 遍历一级目录，order，purch等等
                new_dir = os.path.join(path1, comp)
                dir_path = os.path.join(new_dir.replace(path1,path2), comp)
                for root, dirs, files in os.walk(new_dir): # 按一级目录循环处理后续目录文件
                    #递归创建目录
                    for name in dirs:
                        dir_path = os.path.join(root, name).replace(new_dir, path2)
                        # print(dir_path)
                        if not os.path.isdir(dir_path):
                            os.makedirs(dir_path)
                    #递归拷贝文件
                    for f in files:
                        dep_path = os.path.join(root,f)
                        arr_path = os.path.join(root.replace(new_dir,path2),f)
                        shutil.copy(dep_path,arr_path)
                
        except Exception as e:
            showerror(title = "错误", message = e.args)
        else:
            showinfo(title = "提示", message = "处理完毕!")


def gui_start():
    init_window = Tk()  # 实例化出一个父窗口
    ZMJ_PORTAL = MY_GUI(init_window)
    # 设置根窗口默认属性
    ZMJ_PORTAL.set_init_window()

    init_window.mainloop()  # 父窗口进入事件循环，可以理解为保持窗口运行，否则界面不展示


gui_start()
